<?php
class Application_Model_KhachHang extends Zend_Db_Table
{
	protected $_name='khachhang';
	public function getKhachHang($id)
	{
		$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		if (!$row) {
			throw new Exception("Khong tim thay khach hang $id");
		}
		return $row->toArray();
	}//phuong thuc tra ve mot dong theo $id

	public function generateActivekey($password) {
		try {
			do {
				$key = md5 ( time () );
				$key .= md5 ( $password );
				 
				$sql = 'SELECT activekey FROM khachhang where activekey = "' . $key . '"';
				$db = $this->_db;
				$db->setFetchMode ( Zend_Db::FETCH_OBJ );
				$activekey = $db->fetchAll ( $sql );
			} while ( ! empty ( $activekey ) );
			return $key;
		} catch ( Exception $ex ) {
			Echo ("<br /> Generate Active Key Failed, <br/>Error: " . $ex->getMessage ());
			exit ();
		}
	}//Phát sinh key để kích hoạt tài khoản
	
	public function validateActive($activeKey, $email){
		try {
			$sql = "Select id, trangthai from khachhang where email = ? and activekey = ?";
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_ASSOC );
			$result = $db->fetchAll ( $sql,array($email, $activeKey) );
			if(!empty($result)) return $result[0];
			return $result;
		} catch ( Exception $ex ) {
			echo "<br/>Can not validate activekey, Error: " . $ex->getMessage ();
		}
	}//Kiểm tra lại link kích hoạt
	
	public function updateStatus($id, $status){
		try {
			$data  = array(
					'trangthai' =>  $status
			);
			$this->update($data, 'id = '. (int)$id );
		} catch ( Exception $ex ) {
			echo "<br/>Can not update status for khachhang, Error: " . $ex->getMessage ();
		}
	}//Kích hoạt tài khoản 
	
	public function addKhachHang($tenkhachhang,$diachi,$dienthoai,$email,$username,$pass,$role,$trangthai,$ngaylapacc)
	{
		$data = array(
				'tenkhachhang' => $tenkhachhang,
				'diachi' => $diachi,
				'email' => $email,
				'username' => $username,
				'pass' => $pass,
				'role' => (int)$role,
				'trangthai'=> (int)$trangthai,
				'ngaylayacc'=>date('d-m-Y',$ngaylapacc),
		);
		$this->insert($data);
	}//insert

	public function addKhachHangByArray($data){
		$data['ngaylapacc']=gmdate("Y/m/d", time()+7*3600);
		$this->insert($data);
	}
	
	public function updateCI($data)
	{
		try{
			$this->update($data, 'id = '. (int)$data['id']);
		}catch(Exception $ex){
			echo '<br/>Cannot update information for custommer, Error: '.$ex->getMessage();
			
		}
	}//update

	public function deleteKhachHang($id)
	{
		$this->delete('id =' . (int)$id);
	}//drop


	public function getKhachHangById($id){
		try{
			$sql = 'Select * From `khachhang` where id = '.(int)$id ;
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_OBJ );
			$id = $db->fetchAll ( $sql );
			return $id;
		}catch(Exception $ex){
			echo "Can not get gio hoa by id, Error: ".$ex->getMessage();
			exit;
		}
	}
	public function getKhachHangByUser_Pass($user,$pass){
		try{
			$sql = 'Select * From `khachhang` where username = '.$user.' and pass '.$pass ;
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_ASSOC );
			$id = $db->fetchRow ( $sql );
			return $id;
		}catch(Exception $ex){
			echo "Can not get Khach hang by user and pass, Error: ".$ex->getMessage();
			exit;
		}
	}
}